/**
 * @modal: 智能策略手动&自动执行记录
 */
import CommonModal from "@/components/CommonModal";
import { message, Form, Radio } from "antd";
import { useState, useEffect, forwardRef, useImperativeHandle } from "react";
import Api from "../service";

const operaType = ['smart-split', 'smart-merge', 'auto-audit'];

function OrderScope({ type, item = {}, handleOk, handleCancel }, ref) {

	const [open, setOpen] = useState(false);

	const [form] = Form.useForm();

	//便捷查询池数据
	const [queryList, setQueryList] = useState([]);

	//点否点击确定按钮
	const [isOk, setIsOk] = useState(false);

	useImperativeHandle(ref, () => ({
		queryList: queryList
	}));

	useEffect(() => {
		setOpen(true);
		getExecuteRecord();
	}, []);

	//获取策略列表
	const getExecuteRecord = async () => {
		START_LOADING();
		const { success, data } = await Api.getQueryList(
			 ["sys-order", "hot-order"]
		);
		if (success) {
			setQueryList(data);
		}
		END_LOADING();
	};

	//点击确定
	const onOk = () => {
		const val = form.getFieldsValue();
		if (val.scopeType == 3) {
			if (!val.queryId) {
				message.info("请选择快捷查询");
				return;
			}
		}
		setIsOk(true);
		setOpen(false);
	};

	const onCancel = () => {
		setOpen(false);
	};

	const afterClose = () => {
		if (isOk) {
			const val = form.getFieldsValue();
			handleOk(val);
		} else {
			handleCancel();
		}
	};

	//设置参数
	const options =
		type == 1
			? [
					{ lable: operaType.includes(item.code)  ? '待审核' : "待审核+异常", value: 1 },
					{ lable: "快捷查询池", value: 3 }
				]
			: [
					{ lable: "筛选的订单", value: 2 },
					{ lable: "快捷查询池", value: 3 }
				];

	const scopeType = Form.useWatch("scopeType", form);

	return (
		<CommonModal title="订单范围" afterClose={afterClose} open={open} width="420px" onOk={onOk} onCancel={onCancel}>
			<Form
				form={form}
				name="action-order-scope"
				initialValues={{
					scopeType: item.scopeType,
					queryId: item.queryId
				}}
				autoComplete="off"
			>
				<Form.Item label=" " colon={false} name="scopeType">
					<Radio.Group>
							{options.map(v => (
								<Radio value={v.value} key={v.value}>{v.lable}</Radio>
							))}
					</Radio.Group>
				</Form.Item>
				{scopeType == 3 ? (
					<Form.Item label=" " colon={false} name="queryId">
						<Radio.Group>
							{queryList.map(v => (
								<Radio value={v.id} key={v.id}>
									{v.name}
								</Radio>
							))}
						</Radio.Group>
					</Form.Item>
				) : null}
			</Form>
		</CommonModal>
	);
}

export default forwardRef(OrderScope);
